package com.jd.myaaaccount.model;

import com.jd.myaaaccount.entity.DayBook;
import com.jd.myaaaccount.entity.parameter.AddDayBookParameter;
import com.jd.myaaaccount.entity.result.CommonStringResult;
import com.jd.myaaaccount.entity.result.GetDayBookResult;
import com.jd.myaaaccount.entity.result.SettleResult;
import com.jd.myaaaccount.util.ErrorCode;
import com.jd.myaaaccount.util.HttpHelper;
import com.jd.myaaaccount.util.JsonHelper;
import com.jd.myaaaccount.util.OperateCode;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class DayBookModel {
	private Context context;

	public DayBookModel(Context context) {
		this.context = context;
	}

	public void add(DayBook dayBook, Handler handler) {
		AddDayBookParameter param = new AddDayBookParameter();
		param.setDayBook(dayBook);
		param.setUserid(Common.currentUser.getId());
		param.setToken(Common.token);
		String params = JsonHelper.getString(param);
		String res = HttpHelper.postRequest("daybook/add", params);
		Message msg = new Message();
		msg.what = OperateCode.ADD;
		if (res == "") {
			msg.arg1 = ErrorCode.NET_TIMEOUT;
		} else {
			CommonStringResult csr = JsonHelper.getObject(res,
					CommonStringResult.class);
			Common.resultToMessage(csr, msg);
		}
		handler.sendMessage(msg);
	}

	/**
	 * 获取 流水账
	 * 
	 * @param familyId
	 *            0表示获取个人 真实值表示获取家庭
	 * @param handler
	 */
	public void get(String familyId, Handler handler) {
		String url = String.format(
				"daybook/get/?userId=%s&token=%s&familyId=%s",
				Common.currentUser.getId(), Common.token, familyId);
		String res = HttpHelper.getRequest(url);
		Message msg = new Message();
		msg.what = OperateCode.GET;
		if (res == "") {
			msg.arg1 = ErrorCode.NET_TIMEOUT;
		} else {
			GetDayBookResult gdbr = JsonHelper.getObject(res,
					GetDayBookResult.class);
			Common.resultToMessage(gdbr, msg);
		}
		handler.sendMessage(msg);
	}

	/**
	 * 结算
	 * 
	 * @param handler
	 */
	public void statistic(String familyId, Handler handler) {
		String url = String.format(
				"daybook/statistic/?userId=%s&token=%s&familyId=%s",
				Common.currentUser.getId(), Common.token, familyId);
		String res = HttpHelper.getRequest(url);
		Message msg = new Message();
		msg.what = OperateCode.STATISTIC;
		if (res == "") {
			msg.arg1 = ErrorCode.NET_TIMEOUT;
		} else {
			SettleResult sr = JsonHelper.getObject(res, SettleResult.class);
			Common.resultToMessage(sr, msg);
		}
		handler.sendMessage(msg);
	}

	/**
	 * 上传图片
	 * @param data
	 * @param fileName
	 * @return
	 */
	public String uploadPicture(byte[] data, String fileName) {
		String res = HttpHelper.postFile("daybook/uploadpicture", data,
				fileName);
		if (res == "") {
			return "";
		} else {
			CommonStringResult csr = JsonHelper.getObject(res,
					CommonStringResult.class);
			if (csr.getResult() == ErrorCode.SUCCESS)
				return fileName;
			else
				return "";
		}
	}
}
